﻿using System;
using System.Collections.Generic;

using NTree = NGenerics.DataStructures.Trees;
using NGen = NGenerics;
using NGenGen = NGenerics.DataStructures.General;

namespace Layers.History
{
    internal static class TreeExtensions
    {
        public static NTree.BinaryTree<NGenGen.Association<K, V>> Search<K, V>(this NTree.BinarySearchTreeBase<K, V> tree, IComparable<V> what, SearchOption option)
        {
            return Search<K, V>(tree.RootNode, what, option);
        }

        public static NTree.BinaryTree<NGenGen.Association<K, V>> Search<K, V>(NTree.BinaryTree<NGenGen.Association<K, V>> node, IComparable<V> what, SearchOption option)
        {
            int x = what.CompareTo(node.Data.Value);
            if (x == 0)
                return node;

            NTree.BinaryTree<NGenGen.Association<K, V>> next = x > 0 ? node.Right : node.Left;
            NTree.BinaryTree<NGenGen.Association<K, V>> found;

            if ((found = Search(next, what, option)) != null)
                return found;

            return x * (int)option > 0 ? next : null;
        }

    }
}